package com.sds.service.impl;

import com.sds.service.ThreadStateService;
import org.springframework.stereotype.Service;

@Service
public class ThreadStateServiceImpl implements ThreadStateService {

    @Override
    public void newState() {
        Thread t1 = new Thread(() -> {});
        System.out.println(t1.getState());
    }

    @Override
    public void runnableState() {
        Thread t1 = new Thread(() -> {
            while(true){}
        });
        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getState());
    }

    @Override
    public void blockedState() {
        Object obj = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (obj){}
        });
        synchronized (obj) {
            t1.start();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t1.getState());
        }
    }

    @Override
    public void waitingState() {
        Object obj = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (obj){
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getState());
    }

    @Override
    public void timedWaitingState() {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getState());
    }

    @Override
    public void terminatedState() {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getState());
    }
}